package cdu.javaendwork.Controller.Shopping;

import cdu.javaendwork.model.sqlModel.Goods;
import cdu.javaendwork.model.sqlModel.User;
import cdu.javaendwork.service.Impl.Shopping.GoodsServiceImpl;
import cdu.javaendwork.service.Interface.Shopping.GoodsService;
import cdu.javaendwork.util.pageHelp.PageHelper;
import cdu.javaendwork.util.pageHelp.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/market")
public class MarketController extends HttpServlet {
    GoodsService goodsService = new GoodsServiceImpl();
    PageHelper pageHelper = new PageHelper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sPage=req.getParameter("page");
        int pageSize=3;
        int pageIndex=(sPage == null || sPage.equals("")) ? 1 : Integer.parseInt(sPage);

        HttpSession session = req.getSession();
        User user= (User)session.getAttribute("user");
        int userid = user.getId();
//        System.out.println("userid: " + userid);


        List<Goods> goodsList = goodsService.limit(pageIndex,pageSize);
        PageInfo pageInfo = pageHelper.filter("Goods" , "status != 3" , pageIndex , pageSize);

//        System.out.println(goodsList);
//        System.out.println(pageInfo);
        req.setAttribute("goodsList" ,  goodsList);
        req.setAttribute("pageInfo" , pageInfo);
        req.setAttribute("userid" , userid);

        req.getRequestDispatcher("market.jsp").forward(req,resp);
    }
}
